#!/usr/bin/python

import string
import cgi
import cgitb
import xmlrpclib
import json


class Webgraph:
    def __init__(self):
        self.baseurl = 'http://localhost/api/'

    def site(self,url):
        proxy = xmlrpclib.ServerProxy('http://localhost:9999')
        d = json.loads(str(proxy.site(url.strip())))
        count = d['count']
        result = d['result']
        print '<b>Query:</b>site: %s  <a href="%s">back</a>' % (url,wg.baseurl)
        if count > 0 :
            print '%d pages found in %s' % (count,url)
            print '<hr />'
            for url,title in result:
                if len(title) > 60 :
                    print '<a href="%s">%s...</a>' % (url,title[:50])
                else:
                    print '<a href="%s">%s</a>' % (url,title)
                print '<span style="color:green">%s</span><br />' % (url)
        elif count == -1 :
            print '0 pages found in %s' % (url)
            print '<hr />'
            print '<span style="color:RED">MESSAGE: %s</span><br />' % (result)
        print json.dumps(d, sort_keys=True, indent=4)

    def link(self,url):
        proxy = xmlrpclib.ServerProxy('http://localhost:9999')
        d = json.loads(str(proxy.link(url.strip())))
        count = d['count']
        result = d['result']
        print '<b>Query:</b>link: %s  <a href="%s">back</a>' % (url,wg.baseurl)
        if count > 0 :
            print '%d urls link to %s' % (count,url)
            print '<hr />'
            for url,title in result:
                if len(title) > 60 :
                    print '<a href="%s">%s...</a>' % (url,title[:50])
                else:
                    print '<a href="%s">%s</a>' % (url,title)
                print '<span style="color:green">%s</span><br />' % (url)
        elif count == -1 :
            print '0 pages found in %s' % (url)
            print '<hr />'
            print '<span style="color:RED">MESSAGE: %s</span><br />' % (result)
        print json.dumps(d, sort_keys=True, indent=4)

    def stat(self):
        proxy = xmlrpclib.ServerProxy('http://localhost:9999')
        statdb = json.loads(str(proxy.stat()))
        print '<h2>Statistics</h2>'
        print '<table>'
        for k in statdb:
            print '<tr><td><b>%s</b></td><td>%s</td></tr>' % (k,statdb[k])
        print '</table>'

if __name__ == '__main__' :
    cgitb.enable()
    form = cgi.FieldStorage()

    print "Content-type: text/html\n"
    print "<html><head><title>Simple Webgraph by klainfo</title></head><body>"

    wg = Webgraph()

    if(form.has_key('url')):
        url = form['url'].value.strip()
        print 'Develop by <a href="http://www.klainfo/">klainfo</a> under the granted from <a href="http://www.mikelab.net">Mikelab.net</a>'
        print '<hr>'
        print '<pre style="margin-left:30px">'
        print '<h1>Simple Webgraph by klainfo</h1>'
        if form['method'].value == 'site':
            wg.site(url)
        elif form['method'].value == 'link':
            wg.link(url)
        print '</pre>'

    else:
        print '<div style="font-size:12; text-align:right;" >Develop by <a href="http://www.klainfo/" style="text-decoration:none">klainfo</a> under the granted from <a href="http://www.mikelab.net" style="text-decoration:none">Mikelab.net</a></div>'
        print '<hr style="margin-top:0">'
        print '''
<div id="content" style="margin-top:50px">
<center>
    <h1>Simple Webgraph by klainfo</h1>
    <form method="post" action="">
        <div style="font-size:30px;">
        <select name="method" style="width:100px;height:40px;font-size:30px;" >
        <option value="site">Site: </option>
        <option value="link">Link: </option>
        </select>
        <input name="url" style="width:735px;height:40px;font-size:30px;" value="http://"><br /><pre></pre>
        <input type="submit" style="width:135px;height:40px;font-size:30px;" value="Submit"></div>
    </form>
    <div>
'''
        wg.stat()
        print '''
    </div>
    <p><a href="http://twitter.com/klainfo">Contact @klainfo</a></p>
    <p><a href="http://wiki.klainfo.com/webgraph">Project Wiki</a></p>
    <p><a href="http://www.thaigrid.or.th/">Special thanks to Thai Grid National Project</a></p>
    <p><a href="http://tera.thaigrid.or.th/">Powered by Tera Cluster.</a></p>
</center>
</div>
</body></html>
'''
